<template>
  <div class="maintain-plan-manage">
    <el-card shadow="hover">
      <div class="plan-header">
        <el-input
          v-model="searchText"
          placeholder="搜索计划名称/设备/编号"
          clearable
          style="width:200px; margin-right:16px;"
          @keyup.enter="doSearch"
        />
        <el-select v-model="searchStatus" placeholder="计划状态" clearable style="width:130px; margin-right:16px;">
          <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item"/>
        </el-select>
        <el-button type="primary" @click="handleAdd">新增计划</el-button>
      </div>
      <el-table :data="filteredList" border style="margin-top:16px;">
        <el-table-column prop="id" label="计划编号" width="120px"/>
        <el-table-column prop="name" label="计划名称" width="150px"/>
        <el-table-column prop="device" label="关联设备" width="140px"/>
        <el-table-column prop="planTime" label="计划时间" width="160px"/>
        <el-table-column prop="manager" label="负责人" width="100px"/>
        <el-table-column prop="status" label="计划状态" width="100px">
          <template #default="scope">
            <el-tag :type="statusTagType(scope.row.status)">{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="保养内容" width="200px"/>
        <el-table-column label="操作" width="160px" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" text @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" text @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="520px" destroy-on-close>
      <el-form :model="editRow" :rules="formRules" ref="formRef" label-width="100px">
        <el-form-item label="计划名称" prop="name">
          <el-input v-model="editRow.name"/>
        </el-form-item>
        <el-form-item label="关联设备" prop="device">
          <el-input v-model="editRow.device"/>
        </el-form-item>
        <el-form-item label="计划时间" prop="planTime">
          <el-date-picker v-model="editRow.planTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" style="width:100%;" />
        </el-form-item>
        <el-form-item label="负责人" prop="manager">
          <el-input v-model="editRow.manager"/>
        </el-form-item>
        <el-form-item label="计划状态" prop="status">
          <el-select v-model="editRow.status" placeholder="请选择">
            <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item"/>
          </el-select>
        </el-form-item>
        <el-form-item label="保养内容" prop="content">
          <el-input v-model="editRow.content" type="textarea"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog v-model="deleteVisible" title="提示" width="320px" center>
      <div>确定要删除该计划吗？</div>
      <template #footer>
        <el-button @click="deleteVisible=false">取消</el-button>
        <el-button type="danger" @click="confirmDelete">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

interface MaintainPlan {
  id: string
  name: string
  device: string
  planTime: string
  manager: string
  status: string
  content: string
}

const statusOptions = ['待执行', '执行中', '已完成', '已取消']

// 15条模拟数据
const planList = ref<MaintainPlan[]>([
  { id: 'MP001', name: '中央空调月度保养', device: '中央空调', planTime: '2024-06-10 09:00:00', manager: '张三', status: '待执行', content: '清洗滤网、检查温控' },
  { id: 'MP002', name: '电梯季度保养', device: '电梯', planTime: '2024-06-12 14:00:00', manager: '李四', status: '执行中', content: '润滑安全钳' },
  { id: 'MP003', name: '消防泵检查', device: '消防泵', planTime: '2024-06-13 10:00:00', manager: '王五', status: '已完成', content: '测试泵压力' },
  { id: 'MP004', name: '配电柜维护', device: '配电柜', planTime: '2024-06-14 13:00:00', manager: '张三', status: '待执行', content: '紧固螺丝' },
  { id: 'MP005', name: '净水设备保养', device: '净水设备', planTime: '2024-06-15 15:00:00', manager: '李四', status: '已取消', content: '更换滤芯' },
  { id: 'MP006', name: '新风主机维护', device: '新风主机', planTime: '2024-06-15 16:00:00', manager: '王五', status: '执行中', content: '清洗风道' },
  { id: 'MP007', name: '照明系统排查', device: '照明系统', planTime: '2024-06-16 09:30:00', manager: '张三', status: '待执行', content: '检查灯具、线路' },
  { id: 'MP008', name: '空气净化器维护', device: '空气净化器', planTime: '2024-06-17 08:20:00', manager: '李四', status: '已完成', content: '更换滤网' },
  { id: 'MP009', name: '中央空调冷凝器清洗', device: '中央空调', planTime: '2024-06-18 11:00:00', manager: '王五', status: '待执行', content: '清洗冷凝器' },
  { id: 'MP010', name: '电梯安全系统检查', device: '电梯', planTime: '2024-06-19 10:15:00', manager: '张三', status: '已完成', content: '检测安全钳' },
  { id: 'MP011', name: '净水设备消毒', device: '净水设备', planTime: '2024-06-20 09:30:00', manager: '李四', status: '已完成', content: '设备消毒' },
  { id: 'MP012', name: '消防泵测试', device: '消防泵', planTime: '2024-06-21 09:20:00', manager: '王五', status: '执行中', content: '试运行' },
  { id: 'MP013', name: '空气净化器检修', device: '空气净化器', planTime: '2024-06-22 10:40:00', manager: '张三', status: '已取消', content: '维护传感器' },
  { id: 'MP014', name: '配电柜接地检测', device: '配电柜', planTime: '2024-06-23 08:50:00', manager: '李四', status: '待执行', content: '检测接地线' },
  { id: 'MP015', name: '新风主机功能测试', device: '新风主机', planTime: '2024-06-24 11:30:00', manager: '王五', status: '待执行', content: '功能测试' }
])

// 搜索
const searchText = ref('')
const searchStatus = ref('')
const filteredList = computed(() => {
  let arr = planList.value
  if (searchText.value) {
    arr = arr.filter(
      r =>
        r.name.includes(searchText.value) ||
        r.device.includes(searchText.value) ||
        r.id.includes(searchText.value)
    )
  }
  if (searchStatus.value) {
    arr = arr.filter(r => r.status === searchStatus.value)
  }
  return arr
})
function doSearch() {}

// 新增/编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('新增计划')
const editRow = reactive<MaintainPlan>({
  id: '',
  name: '',
  device: '',
  planTime: '',
  manager: '',
  status: '',
  content: ''
})
const formRef = ref()
const formRules = {
  name: [{ required: true, message: '必填', trigger: 'blur' }],
  device: [{ required: true, message: '必填', trigger: 'blur' }],
  planTime: [{ required: true, message: '必填', trigger: 'change' }],
  manager: [{ required: true, message: '必填', trigger: 'blur' }],
  status: [{ required: true, message: '必选', trigger: 'change' }]
}

function handleAdd() {
  dialogTitle.value = '新增计划'
  Object.assign(editRow, { id: '', name: '', device: '', planTime: '', manager: '', status: '', content: '' })
  dialogVisible.value = true
}
function handleEdit(row: MaintainPlan) {
  dialogTitle.value = '编辑计划'
  Object.assign(editRow, row)
  dialogVisible.value = true
}
function submitEdit() {
  formRef.value?.validate((valid: boolean) => {
    if (!valid) return
    if (dialogTitle.value.startsWith('编辑')) {
      const idx = planList.value.findIndex(t => t.id === editRow.id)
      if (idx !== -1) planList.value[idx] = { ...editRow }
      ElMessage.success('编辑成功')
    } else {
      // 自动生成新编号
      editRow.id = 'MP' + (planList.value.length + 1).toString().padStart(3, '0')
      planList.value.unshift({ ...editRow })
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
  })
}

// 标签类型
const statusTagType = (status: string | undefined): 'success' | 'warning' | 'danger' | 'info' | 'primary' | undefined => {
  switch (status) {
    case '待执行': return 'warning'
    case '执行中': return 'primary'
    case '已完成': return 'success'
    case '已取消': return 'info'
    default: return 'primary'
  }
}

// 删除
const deleteVisible = ref(false)
const deleteTarget = ref<MaintainPlan | null>(null)
function handleDelete(row: MaintainPlan) {
  deleteTarget.value = row
  deleteVisible.value = true
}
function confirmDelete() {
  if (deleteTarget.value) {
    planList.value = planList.value.filter(t => t.id !== deleteTarget.value!.id)
    ElMessage.success('删除成功')
  }
  deleteVisible.value = false
}
</script>

<style scoped>
.maintain-plan-manage {
  width: 100%;
  margin: 0;
  padding: 32px 0;
}
.plan-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
</style>
